﻿#include "mainwidget.h"
#include "./ui_mainwidget.h"
#include "addcommodity.h"
#include "inbound.h"
#include "delivery.h"
#include "datasum.h"

MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MainWidget)
{
    ui->setupUi(this);

    //设置主窗口背景颜色
    QPalette plt;
    plt.setColor(QPalette::Window,QColor(180,180,180));
    this->setPalette(plt);
    ConnectMySQLDBFunc();

    TableWidgetInit();
}

MainWidget::~MainWidget()
{
    delete ui;
}


void MainWidget::ConnectMySQLDBFunc()
{
    QSqlDatabase db=QSqlDatabase::addDatabase("QODBC");
    db.setHostName("127.0.0.1");
    db.setPort(3306);
    db.setDatabaseName("stockmsdb");
    db.setUserName("root");
    db.setPassword("123456");

    bool is_success=db.open();

    if(!is_success)//判断连接数据库是否成功
    {
        QMessageBox::information(this,"提示","数据库连接失败");
    }
}

//新增商品
void MainWidget::on_AddBtn_clicked()
{
    AddCommodity *addlg=new AddCommodity();
    addlg->show();
}

//删除商品
void MainWidget::on_DeleteBtn_clicked()
{
    int iclick=QMessageBox::question(this,"警告","确认要删除该商品？");

    if(iclick==QMessageBox::Yes)
    {
        int irow=ui->ListDATA->currentRow();

        QString strValue=ui->ListDATA->model()->index(irow,0).data().toString();
        //QMessageBox::information(this,"测试",strValue);

        QSqlQuery result;
        QString strid=strValue;

        if(strid.isEmpty())
        {
            QMessageBox::critical(this,"错误","请选择商品编号");
            return;
        }
        QString sqlquery=QString("delete from commoditydatatable where StockId=%1").arg(strid);
        if(result.exec(sqlquery))
        {
            QMessageBox::information(this,"提示","删除成功");

            ui->ListDATA->clear();
            TableWidgetInit();
        }
    }
    else
    {
        return ;
    }
}

//商品入库
void MainWidget::on_StoreBtn_clicked()
{
    Inbound *inlg=new Inbound();
    inlg->show();


}

//商品出库
void MainWidget::on_OutboundBtn_clicked()
{
    Delivery *deliverlg=new Delivery();
    deliverlg->show();
}
//导出数据:生成excel问价并保存
void MainWidget::on_ExportDataBtn_clicked()
{
    // 保存文件扩展名为.xls
    QDateTime time;
    QString strTemp;

    time=QDateTime::currentDateTime();//获取系统时间作为文件名
    strTemp=time.toString("yyyy-MM-dd-hhmmss");

    //QMessageBox::information(this,"测试",strTemp);
    QString strfilename=QFileDialog::getSaveFileName(this,tr("Excel Files"),QString("%1%2.xls").arg(strTemp,"-库存信息"),tr("Excel Files(*.xls)"));
    //QMessageBox::information(this,"测试",strfilename);
    if(!strfilename.isEmpty())
    {
        QAxObject *Excel=new QAxObject;
        if(Excel->setControl("Excel.Application"))
        {
            Excel->dynamicCall("SetVisible (bool Visible)",false);
            Excel->setProperty("DisplayAlerts",false);

            QAxObject *workbooks=Excel->querySubObject("WorkBooks");
            workbooks->dynamicCall("Add");
            QAxObject *workbook=Excel->querySubObject("ActiveWorkBook");
            QAxObject *worksheet=workbook->querySubObject("Worksheets(int)",1);
            QAxObject *cell;
            for(int i=1;i<=ui->ListDATA->columnCount();i++)
            {
                cell=worksheet->querySubObject("Cells(int,int)",1,i);
                cell->setProperty("RowHeight",38);
                cell->dynamicCall("SetValue(const QString&)",ui->ListDATA->horizontalHeaderItem(i-1)->data(0).toString());
            }
            for(int i=2;i<=ui->ListDATA->rowCount()+1;i++)
            {
                for(int j=1;j<=ui->ListDATA->columnCount();j++)
                {
                    /*cell=worksheet->querySubObject("Cells(int,int)",i,j);
                    cell->dynamicCall("SetValue(const QString&)",ui->ListDATA->item(i-2,j-1)->text()+"\t");*/
                    QTableWidgetItem *item = ui->ListDATA->item(i - 2, j - 1);
                    QString text = item ? item->text() + "\t" : "";
                    QAxObject *cell = worksheet->querySubObject("Cells(int,int)", i, j);
                    cell->dynamicCall("SetValue(const QString&)", text);
                }
            }

            workbook->dynamicCall("SaveAs(const QString&)",QDir::toNativeSeparators(strfilename));
            workbook->dynamicCall("Close()");
            Excel->dynamicCall("Quit()");
            delete Excel;
            Excel=NULL;
        }
    }
}

//数据汇总
void MainWidget::on_SummaryBtn_clicked()
{
    DataSum *sublg=new DataSum();
    sublg->show();
}


void MainWidget::on_SearchBtn_clicked()
{
    ui->ListDATA->clear();
    my_Init();
    if(ui->NumberlEt->text().isEmpty())
    {
        my_search("select *from commoditydatatable");
    }
    else
    {
        QString strid="stockId=";

        strid=strid+ui->NumberlEt->text();

        QString str=QString("select *from commoditydatatable where %1").arg(strid);

        my_search(str);
    }

}
//表单初始化函数
void MainWidget::TableWidgetInit()
{
    my_Init();
    my_search("select *from commoditydatatable");


}
void MainWidget::my_search(QString str)
{
    QSqlQuery sql;
    sql.exec(str);

    int i=0;
    while(sql.next())
    {
        QString strId=sql.value(0).toString();
        QString strName=sql.value(1).toString();
        QString strAmount=sql.value(2).toString();
        QString strUnitPrice=sql.value(3).toString();
        QString strSupplier=sql.value(4).toString();
        QString strDirector=sql.value(5).toString();
        QString strWareHouseTime=sql.value(6).toString();
        QString strDeliveryTime=sql.value(7).toString();
        QString strRemarks=sql.value(8).toString();

        ui->ListDATA->setItem(i,0,new QTableWidgetItem(strId));
        ui->ListDATA->setItem(i,1,new QTableWidgetItem(strName));
        ui->ListDATA->setItem(i,2,new QTableWidgetItem(strAmount));
        ui->ListDATA->setItem(i,3,new QTableWidgetItem(strUnitPrice));
        ui->ListDATA->setItem(i,4,new QTableWidgetItem(strSupplier));
        ui->ListDATA->setItem(i,5,new QTableWidgetItem(strDirector));
        ui->ListDATA->setItem(i,6,new QTableWidgetItem(strWareHouseTime));
        ui->ListDATA->setItem(i,7,new QTableWidgetItem(strDeliveryTime));
        ui->ListDATA->setItem(i,8,new QTableWidgetItem(strRemarks));
        i++;
    }
}

void MainWidget::my_Init()
{
    ui->ListDATA->setColumnCount(9);
    ui->ListDATA->setRowCount(200);
    ui->ListDATA->setHorizontalHeaderLabels
        (QStringList()<<"商品编号"<<"名称"<<"数量"<<"单价"<<"供应商家"<<"负责人"<<"入库时间"<<"出库时间"<<"备注");

    ui->ListDATA->setColumnWidth(0,80);
    ui->ListDATA->setColumnWidth(1,150);
    ui->ListDATA->setColumnWidth(2,80);
    ui->ListDATA->setColumnWidth(3,80);
    ui->ListDATA->setColumnWidth(4,300);
    ui->ListDATA->setColumnWidth(5,80);
    ui->ListDATA->setColumnWidth(6,250);
    ui->ListDATA->setColumnWidth(7,250);
    ui->ListDATA->setColumnWidth(8,180);
}
